/*============================================================================
 PROJECT: DealHits
 FILE:    MainViewController.m
 AUTHOR:  Ho Huu Tai
 DATE:    7/8/13
 =============================================================================*/

/*============================================================================
 IMPORT
 =============================================================================*/
#import "MainViewController.h"
#import "LeftMenuViewController.h"
#import "HomeViewController.h"
#import "RightMenuViewController.h"
#import "ListBookViewController.h"
#import "ListAuthorViewController.h"
/*============================================================================
 PRIVATE MACRO
 =============================================================================*/
#define kRootViewControllerName               @"name"
#define kRootViewControllerStoryName          @"story"
#define kRootViewControllertitle              @"title"
#define kRootViewControllerSelectedImage      @"selectedImage"
#define kRootViewControllerUnSelectedImage    @"unSelectedImage"
#define kRootViewControllerTag                @"tag"
#define kRadiusCircle         100

typedef NS_ENUM(NSInteger, TabbarItemViewControllerTag) {
    kTabbarItemViewControllerTagMessage = 0,
    kTabbarItemViewControllerTagContact,
    kTabbarItemViewControllerTagPlsAssist,
    kTabbarItemViewControllerTagOutAbout,
    kTabbarItemViewControllerTagSettings
};
/*============================================================================
 PRIVATE INTERFACE
 =============================================================================*/

@interface MainViewController ()
@property (nonatomic,weak) IBOutlet UIView *menuView;
@property (nonatomic,weak) IBOutlet UIButton *btnShowMenu;
@property (nonatomic,weak) IBOutlet UIButton *btnDownload;
@property (nonatomic,weak) IBOutlet UIButton *btnList;
@property (nonatomic,weak) IBOutlet UIButton *btnEditor;
@property (nonatomic,weak) IBOutlet UIButton *btnFavorite;
@property (nonatomic,strong) ListBookViewController *listBook;
@property (nonatomic,strong) ListAuthorViewController *listAuthor;
@end

@implementation MainViewController

#pragma mark - Settings for each kind: UITabbarController, UINavigationController, PKRevealViewController
/* For UITabbarViewController */
#pragma mark - Customize Subview
-(void)customizeSubView{
    /* set frame show menu */
//    self.menuView.frame = CGRectMake(10, self.view.bounds.size.height - 115, self.menuView.frame.size.width, self.menuView.frame.size.height);
    
    /* set frame for button menu*/
    CGPoint pointCenter = self.btnShowMenu.center;
    self.btnDownload.center = [self pointAroundCircumferenceFromCenter:pointCenter
                                                            withRadius:kRadiusCircle
                                                              andAngle:0];
    self.btnList.center = [self pointAroundCircumferenceFromCenter:pointCenter
                                                        withRadius:kRadiusCircle
                                                          andAngle:30];
    self.btnEditor.center = [self pointAroundCircumferenceFromCenter:pointCenter
                                                          withRadius:kRadiusCircle
                                                            andAngle:60];
    self.btnFavorite.center = [self pointAroundCircumferenceFromCenter:pointCenter
                                                            withRadius:kRadiusCircle
                                                              andAngle:90];
    [self hideMenu];
    
    /* set selected for button */
    [self.btnDownload setImage:ImgWithName(@"icon2") forState:UIControlStateHighlighted];
    [self.btnList setImage:ImgWithName(@"icon3") forState:UIControlStateHighlighted];
    [self.btnEditor setImage:ImgWithName(@"icon4") forState:UIControlStateHighlighted];
    [self.btnFavorite setImage:ImgWithName(@"icon5") forState:UIControlStateHighlighted];
}
-(IBAction)didTouchedShowMenu:(UIButton*)sender{
    
    self.btnShowMenu.selected = !sender.selected;
    if (self.btnShowMenu.selected) {
        [self rotateMenu];
        [self showMenu];
    }else{
        [self hideMenu];
        [self reverseMenu];
    }
}
-(UIViewController*)currentController{

    BaseNavigationController * sdasd = (BaseNavigationController*)self.frontViewController;
    UIViewController* root = sdasd.topViewController;
    return root;
}
-(IBAction)didTouchedList:(UIButton*)sender{
    
    if ([[self currentController] isKindOfClass:[ListBookViewController class]]) {
        return;
    }
    BaseNavigationController * sdasd = (BaseNavigationController*)self.frontViewController;
    [sdasd popToRootViewControllerAnimated:YES];
    
    [(UINavigationController*)self.frontViewController pushViewController:self.listBook animated:YES];
}
-(ListBookViewController *)listBook{
    if (!_listBook) {
        _listBook = [[IBHelper sharedUIHelper]loadViewController:@"ListBookVC" inStory:@"AloTruyen"];
    }
    return _listBook;
}
-(IBAction)didTouchedDownload:(UIButton*)sender{
    
}
-(IBAction)didTouchedEditor:(UIButton*)sender{
    if ([[self currentController]isKindOfClass:[ListAuthorViewController class]]) {
        return;
    }
    BaseNavigationController * sdasd = (BaseNavigationController*)self.frontViewController;
    [sdasd popToRootViewControllerAnimated:YES];
    BaseViewController *authorVC = [[IBHelper sharedUIHelper]loadViewController:@"ListAuthorVC" inStory:@"AloTruyen"];
    [(UINavigationController*)self.frontViewController pushViewController:authorVC animated:YES];
}
-(IBAction)didTouchedFavorite:(UIButton*)sender{
    
}
#pragma mark - Helper Method
-(void)rotateMenu{
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.btnShowMenu.transform = CGAffineTransformMakeRotation(M_PI_4);
    } completion:^(BOOL finished) {
    }];
}
-(void)reverseMenu{
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.btnShowMenu.transform = CGAffineTransformMakeRotation(M_PI);
    } completion:^(BOOL finished) {
    }];
}
- (CGPoint)pointAroundCircumferenceFromCenter:(CGPoint)center withRadius:(CGFloat)radius andAngle:(CGFloat)theta
{
    CGPoint point = CGPointZero;
    point.x = center.x + radius * cos((theta-90) * M_PI / 180);
    point.y = center.y + radius * sin((theta-90) * M_PI / 180);
    return point;
}
-(void)showMenu{
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.btnFavorite.alpha = 1;
        self.btnEditor.alpha = 1;
        self.btnDownload.alpha = 1;
        self.btnList.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
}
-(void)hideMenu{
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        /* hide all button menu */
        self.btnList.alpha = 0;
        self.btnFavorite.alpha = 0;
        self.btnEditor.alpha = 0;
        self.btnDownload.alpha = 0;
    } completion:nil];
}

#pragma mark - Initialization

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

#pragma mark - View Lifecyle
//- (void)showViewController:(UIViewController *)controller{
//    [super showViewController:controller
//                     animated:YES
//                   completion:^(BOOL finished) {
//                       if (finished) {
//                           [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//                               self.menuView.hidden = !self.menuView.hidden;
//
//                           } completion:^(BOOL finished) {
//                               
//                           }];
//                       }
//    }];
//}
-(void)showViewController:(UIViewController *)controller
                 animated:(BOOL)animated
               completion:(PKDefaultCompletionHandler)completion{
    [super showViewController:controller
                     animated:animated
                   completion:^(BOOL finished) {
                       BaseNavigationController *nav = (BaseNavigationController*)controller;
                       UIViewController *controller = nav.topViewController;
                       if ([controller isKindOfClass:[LeftMenuViewController class]]) {
                           self.menuView.hidden = YES;
                       }else{
                           self.menuView.hidden = NO;
                       }
                       NSString *title = nav.title;
                       NSLog(@"title : %@",title);
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    /* setup main view controller */
    @autoreleasepool {
        LeftMenuViewController*leftVC = [[IBHelper sharedUIHelper]loadViewController:@"LeftMenuVC" inStory:@"AloTruyen"];
        UINavigationController *navLeftVC = [[BaseNavigationController alloc]initWithRootViewController:leftVC];
        self.leftViewController = navLeftVC;
        
        
        HomeViewController *home = [[IBHelper sharedUIHelper]loadViewController:@"HomeVC" inStory:@"AloTruyen"];
        self.aloTruyenNav = [[BaseNavigationController alloc]initWithRootViewController:home];
        self.frontViewController = self.aloTruyenNav;
        
        [self.view bringSubviewToFront:self.menuView];
        [self customizeSubView];
        /*
         * By Pass in version 1.0

        RightMenuViewController *rightVC = [[IBHelper sharedUIHelper]loadViewController:@"RightMenuVC" inStory:@"AloTruyen"];
        UINavigationController *navRightVC = [[BaseNavigationController alloc]initWithRootViewController:rightVC];
        self.rightViewController = navRightVC;
        
        self.currentNav = _aloTruyenNav;
        */
        
        /* register authentication notification */
        NotifReg(self, @selector(handleAuthenticatedNotification:), KAuthenticatedNotification);

    }
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Gesture Event

- (void)handleFingerPinch:(UIPinchGestureRecognizer *)gesture {
    NSLog(@"Pinch scale: %f", gesture.scale);
    CGAffineTransform transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale);
    // you can implement any int/float value in context of what scale you want to zoom in or out
    
    self.view.transform = transform;
}

#pragma mark - Main methods

- (void)startUp:(BOOL)animate {
    
}

- (void)handleAuthenticatedNotification:(NSNotification *)notification {
    [self startUp:YES];
}

#pragma mark - Application event

- (void)applicationDidFinishedLauching {
    
}

- (void)applicationDidBecomeActive {
    
}

- (void)applicationWillResignActive {
    
}

- (BOOL)handleOpenURL:(NSURL *)url {
    return YES;
}

- (void)applicationWillTerminate {
    
}

- (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
}

- (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    
}

- (void)didReceiveRemoteNotification:(NSDictionary *)userInfo {
    
}

- (void)didReceiveLocalNotification:(UILocalNotification *)notification {
    
}

- (void)applicationDidEnterBackground {
    
}

- (void)applicationWillEnterForeground {
    
}

@end
